package LandR.personal.personalServlet;

import LandR.personal.personalHelp.Users.User;
import LandR.personal.personalHelp.function_jsp.JspPageUseHelpMethod;
import LandR.personal.personalHelp.function_jsp.JspPageUseMethod;
import token.TokenHelp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.sql.SQLException;

@WebServlet("/GetImgServlet")
public class GetImgServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("image/png");
        if (request.getMethod().equals("GET")) {
            response.setContentType("image/png");
            User user = new User();
            //获取令牌
            String token = request.getHeader("token");
            token = token.substring(token.indexOf("\"") + 1, token.lastIndexOf("\""));
            System.out.println("前端传递的token令牌是：" + token);
            TokenHelp th = new TokenHelp();
            Integer ID = -1;
            try {
                ID = th.judgeToken(token);
            } catch (SQLException e) {
                e.printStackTrace();
            }

            if (ID != -1) {

                user.setId(ID);
                JspPageUseHelpMethod jphm = new JspPageUseHelpMethod();
                JspPageUseMethod jpum = new JspPageUseMethod();
                try {
                    user = jpum.selectUserData(user);
                    String imageAddress = user.getImageAddress();
                    File file = new File(imageAddress);
                    FileInputStream fis = new FileInputStream(file);
                    BufferedInputStream bis = new BufferedInputStream(fis);
                    long size = file.length();
                    byte[] temp = new byte[(int) size];
                    bis.read(temp, 0, (int) size);
                    System.out.println("成功获取头像的信息");
                    bis.close();
                    fis.close();
                    byte[] data = temp;
                    OutputStream out = response.getOutputStream();
                    out.write(data);
                    out.flush();
                    out.close();
                    fis.close();
                    PrintWriter output = response.getWriter();
                    output.print("头像上传成功！");
                    System.out.println("头像上传成功！");
                    System.out.println("------图片信息已经传递给前端！");
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            } else {
                System.out.println("失败！token已过期！");
                PrintWriter out = response.getWriter();
                out.print("失败！token已过期");
            }

        } else {
            PrintWriter out = response.getWriter();
            System.out.println("失败！查询方法不匹配！");
            out.println("失败！查询方法不匹配！");

        }

    }
}



